﻿' Copyright (c) Microsoft Corporation.  All rights reserved.
Imports System.Data.EntityClient
Imports EdmMetadataHelpersVB

Public Class eSQLExecutor



    ''' <summary>
    ''' Execute the eSQL in the txtESQL textbox and convert the results to XML
    ''' and show the XML in the txtResults textbox.
    ''' </summary>
    Private Sub btnExecuteXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExecuteXML.Click
        HideAll()

        Using connection As New EntityConnection("Name=NorthwindEntities")
            connection.Open()
            Using command As New EntityCommand(txtESQL.Text.Trim(), connection)
                Using reader = command.ExecuteReader(CommandBehavior.SequentialAccess)
                    txtResults.Text = New XElement("Results", EdmXmlAdapter.GetElements(reader)).ToString()
                End Using
            End Using
        End Using

        txtResults.Visible = True
    End Sub

    ''' <summary>
    ''' Execute the eSQL in the txtESQL textbox and convert the results to a DataTable
    ''' and bind those results to the dgResults datagrid.
    ''' </summary>
    Private Sub btnExecuteDataTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExecuteDataTable.Click
        HideAll()

        Using connection As New EntityConnection("Name=NorthwindEntities")
            connection.Open()
            Using command As New EntityCommand(txtESQL.Text.Trim(), connection)
                Using reader = command.ExecuteReader(CommandBehavior.SequentialAccess)
                    dgResults.DataSource = EdmDataReaderAdapter.LoadTable(reader)
                End Using
            End Using
        End Using

        dgResults.Visible = True
    End Sub

    ''' <summary>
    ''' Hide all results
    ''' </summary>
    Private Sub HideAll()
        txtResults.Visible = False
        dgResults.Visible = False
    End Sub
End Class
